package me.gacl.mapping;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import me.gacl.domain.Blog;
import me.gacl.domain.User;

public interface BlogMapper {
	@Insert("insert into blog(user_id, title, content) value(#{userId}, #{title}, #{content})")
	@Options(keyProperty="id", useGeneratedKeys=true)
	Integer insert(Blog blog);
	@Update("update blog set user_id = #{userId}, title=#{title}, content=#{content} where id=#{id}")
	Integer update(Blog blog);
	@Delete("delete from blog where id=#{id}")
	Integer delete(Integer id);
	@Select("select * from blog")
	@Results(value={
			@Result(property="id", column="id"),
			@Result(property="userId", column="user_id"),
			@Result(property="title", column="title"),
			@Result(property="content", column="content"),
			@Result(property="author", column="user_id", javaType=User.class, one=@One(select="me.gacl.mapping.userMapper.getUser"))}
			)
	List<Blog> selectAll();
	@Select("select b.*,u.name as user_name from blog as b, user as u where id=#{id} and b.user_id=u.user_id")
	@Results(value={
			@Result(property="id", column="id"),
			@Result(property="userId", column="user_id"),
			@Result(property="title", column="title"),
			@Result(property="content", column="content"),
			@Result(property="authorName", column="user_name")
			})
	Blog selectById(Integer id);
	
	@Select("select * from blog where user_id=#{id}")
	@Results(value={
			@Result(property="id", column="id"),
			@Result(property="userId", column="user_id"),
			@Result(property="title", column="title"),
			@Result(property="content", column="content"),
			@Result(property="author", column="user_id", javaType=User.class, many=@Many(select="me.gacl.mapping.userMapper.getUser"))
			})
	List<Blog> findBlogByUserId(Integer id);
}
